﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dryricenet
{
    public partial class home_Details : System.Web.UI.Page
    {
        public int foodid = 0;
        public int xs = 0;//销售量
        public int kc = 0;//库存
        public double price = 0.0;//单价
        public string fen = "";//美食份
        public string foodTypeName = "";//美食类别
        public string Describe = "";//美食描述
        public string Time = "";//上架时间
        public string FoodName = "";//上架时间
        public string PicUrl = "";//美食图片
        /// <summary>
        /// 评价内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public string Content = "";//评价内容
        public string AppTime = "";//评价时间
        public string UserName = "";//评价姓名
        public string Anonymous = "";//匿名
        public string no = "";//没有评价
        public string num = "";//被评价次数

        protected void Page_Load(object sender, EventArgs e)
        {
            foodid = Convert.ToInt32(Request.QueryString["FoodID"].ToString());
            DataTable dt = foodBLL.selectIdBLL(foodid);
            DataRow dr = dt.Rows[0];//第一条数据
            xs = Convert.ToInt32(dr["SalesVolume"]);
            kc = Convert.ToInt32(dr["Inventory"]);
            price = Convert.ToDouble(dr["Price"]);
            fen = dr["Price"].ToString();
            foodTypeName = dr["FoodTypeName"].ToString();
            Describe = dr["Describe"].ToString();
            Time = dr["Time"].ToString();
            FoodName = dr["FoodName"].ToString();
            string picurl = dr["PicUrl"].ToString();
            PicUrl = picurl.Substring(1);
            ///
            /// 评价查看
            num = AppraiseBLL.numBLL(foodid).ToString();
            if (AppraiseBLL.sfPJBLL(foodid).Rows.Count > 0)
            {
                DataTable dt1 = AppraiseBLL.appraiseCKBLL(foodid);
                DataRow dr1 = dt1.Rows[0];//第一条数据
                Content = dr1["Content"].ToString();
                AppTime = dr1["Time"].ToString();
                string name = dr1["UserName"].ToString();
                Anonymous = dr1["Anonymous"].ToString();
                if (Anonymous == "（匿名）")
                {

                    UserName = name.Replace(name.Substring(1), "*");
                }
                else {
                    UserName = dr1["UserName"].ToString();
                }
            }
            else {
                no = "亲！这个美食还没有被评价哦！赶快去尝试一下吧！";
            }
           
            if (Session["Name"] == null)
            {
                //登录状态为空
                Response.Write("<script>javascript:alert('对不起,您还没有登录!');history.back();</script>");
                Response.End();
            }
            else if (!IsPostBack)
            {
                DataList1.DataSource = foodBLL.selectIdBLL(foodid);
                DataList1.DataBind();
                ViewState["countState"] = 1;
               
            }
        }
        //加入购物车
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = Session["Name"].ToString();
            DataTable dt = userBLL.selectNameBll(name);
            DataRow dr = dt.Rows[0];//第一条数据
            int userID = Convert.ToInt32(dr["UserID"].ToString());
            //int foodId = Convert.ToInt32(Request.QueryString["FoodID"]);
            //double price = Convert.ToDouble(foodBLL.pricesBLL(foodid));
            int sl = int.Parse(txtSZ.Text.Trim());
            Cart c = new Cart();
            c.UserID = userID;
            c.FoodID = foodid;
            c.Number = sl;
            c.Total = sl * price;
            //更改销售量和库存
            //根据id查询数据，并展示在输入框中
            //DataTable dt2 = foodBLL.xskcBLL(foodid);
            ////用DataRow来存一行数据
            //DataRow dr2 = dt2.Rows[0];//第一条数据
            //                          //把数据一个一个的放进输入框里
            //xs = Convert.ToInt32(dr2["SalesVolume"]);
            //kc = Convert.ToInt32(dr2["Inventory"]);
            xs = xs + sl;
            kc = kc - sl;
            if (kc >= 0)
            {
                foodBLL.updateKCXLBLL(xs, kc, foodid);
                if (CartBLL.cartAddBLL(c))
                {
                    DataList1.DataSource = foodBLL.selectIdBLL(foodid);
                    DataList1.DataBind();
                    Response.Write("<script>alert('添加成功！')</script>");
                }
                else
                {
                    Response.Write("<script>alert('添加失败！')</script>");
                }
            }
            else {
                Response.Write("<script>alert('亲！库存不足请换件美食吧！')</script>");
            }
        }
        //加
        protected void Button3_Click(object sender, EventArgs e)
        {
            //ViewState：当我们把值存给它以后，值的类型就编译了object类型
            int count = (int)ViewState["countState"];//当我
            count++;
            txtSZ.Text = count.ToString();
            ViewState["countState"] = count;//再把相加后的值，赋给ViewState对象
        }
        //减
        protected void Button2_Click(object sender, EventArgs e)
        {
            //ViewState：当我们把值存给它以后，值的类型就编译了object类型
            int count = (int)ViewState["countState"];//当我
            if (count > 1)
            {
                count--;
            }
            txtSZ.Text = count.ToString();
            ViewState["countState"] = count;//再把相加后的值，赋给ViewState对象
        }

        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //立即下单
        protected void Button4_Click(object sender, EventArgs e)
        {
            string name = Session["Name"].ToString();
            DataTable dt = userBLL.selectNameBll(name);
            DataRow dr = dt.Rows[0];//第一条数据
            int userID = Convert.ToInt32(dr["UserID"].ToString());
            //int foodId = Convert.ToInt32(Request.QueryString["FoodID"]);
            //double price = Convert.ToDouble(foodBLL.pricesBLL(foodId));
            int sl = int.Parse(txtSZ.Text.Trim());
            Cart c = new Cart();
            c.UserID = userID;
            c.FoodID = foodid;
            c.Number = sl;
            c.Total = sl * price;
            //更改销售量和库存
            //根据id查询数据，并展示在输入框中
            //DataTable dt2 = foodBLL.xskcBLL(foodid);
            ////用DataRow来存一行数据
            //DataRow dr2 = dt2.Rows[0];//第一条数据
            //                          //把数据一个一个的放进输入框里
            //xs = Convert.ToInt32(dr2["SalesVolume"]);
            //kc = Convert.ToInt32(dr2["Inventory"]);
            xs = xs + sl;
            kc = kc - sl;
            if (kc >= 0)
            {
                foodBLL.updateKCXLBLL(xs, kc, foodid);
                if (CartBLL.cartAddBLL(c))
                {
                    DataList1.DataSource = foodBLL.selectIdBLL(foodid);
                    DataList1.DataBind();
                    Response.Redirect("home_Order.aspx");
                }
                else
                {
                    Response.Write("<script>alert('立即下单失败！')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('亲！库存不足请换件美食吧！')</script>");
            }
        }
    }
}